package TCP;

import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;

public class Day11_Server {//主类开始
    //图形界面
    //1.底层框架
    JFrame jFrame = new JFrame();
    //2.显示区域
    JTextArea jTextArea = new JTextArea();
    //3.滚动效果
    JScrollPane jScrollPane =
            new JScrollPane(jTextArea);

    //3.Socket顶点
    Socket socket = null;
    //4.ServerSocket顶点
    ServerSocket serverSocket = null;
    //5.端口号
    int port = 8888;
    //6.Inputstream
    //InputStream inputStream = null;
    //构造容器来存储客户端信息
    ArrayList<ThreadServer> arrayList =
            new ArrayList<>();

	/*List<ThreadServer>  list =
			new ArrayList<>();

	ArrayList<Socket> arrayList2 =
			new ArrayList<>();*/


    //初始化界面
    public void init_server_window() {

        jFrame.setVisible(true);
        jFrame.setBounds(300, 300, 400, 400);

        Font font =
                new Font("宋体", Font.BOLD, 30);
        jTextArea.setFont(font);

        jScrollPane.
                setVerticalScrollBarPolicy
                        (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        //自动显示文字在最底部
        DefaultCaret defaultCaret =
                (DefaultCaret)
                        jTextArea.getCaret();

        defaultCaret.setUpdatePolicy
                (DefaultCaret.ALWAYS_UPDATE);


        jFrame.add(jScrollPane);//jTextArea
        jTextArea.setEditable(false);
        jFrame.setTitle("服务器");

    }

    //接收客户端发来的数据
    public void getMessage() {

        //架设服务器
        try {
            serverSocket =
                    new ServerSocket(port);

            while (true) {
                socket = serverSocket.accept();
	/*System.out.println("一个客户端加入连接："+
			socket.getPort());*/
                jTextArea.append("服务器信息：一个客户端连接服务器" +
                        socket.getInetAddress() + "/" + socket.getPort() + '\n');

                //启动线程：     socket-线程
                ThreadServer threadServer =
                        new ThreadServer(socket);
                //Thread thread = new Thread(threadServer);

                arrayList.add(threadServer);

                threadServer.start();

            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


    public static void main(String[] args) {//主函数开始
        //
        Day11_Server server = new Day11_Server();

        server.init_server_window();

        server.getMessage();

    }//主函数结束

    //创建内部类ThreadServer用来实现线程功能
    class ThreadServer extends Thread {
        Socket socket;

        public ThreadServer(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            //接收数据
            try {
                InputStream inputStream =
                        socket.getInputStream();

                byte[] array = new byte[1024];
                int len;
                String string;

                while ((len = inputStream.read(array)) != -1) {
                    string = new String
                            (array, 0, len);
                    jTextArea.append(string);

                    //服务器转发数据
                    Iterator<ThreadServer> iterator =
                            arrayList.iterator();

                    while (iterator.hasNext()) {
                        Day11_Server.ThreadServer threadServer =
                                (Day11_Server.ThreadServer) iterator.next();

                        if (threadServer.socket != socket) {
                            threadServer.sendmessage(string);
                        }

                    }


                }


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // TODO Auto-generated method stub
            //super.run();
        }

        private void sendmessage(String string) {
            // 发送数据
            try {
                OutputStream outputStream =
                        socket.getOutputStream();

                outputStream.write(string.getBytes());

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }


}//主类结束
